import 'dart:io';

import 'package:flutter/services.dart';
import 'package:jd_flutter/base/utils/jd_log.dart';

class JDPluginInstance {
  // 推送点击
  static const String kPushClick = 'pushClick';

  static const MethodChannel tdChannel = MethodChannel('jd_learn_plugin');

  factory JDPluginInstance() => _instance;
  static final JDPluginInstance _instance = JDPluginInstance._internal();

  JDPluginInstance._internal() {
    //app调用flutter方法处理
    tdChannel.setMethodCallHandler((call) {
      if (call.method == kPushClick) {
        jdLog("native调用了flutter ${call.arguments}");
        if (call.arguments is Map) {
          dealPushClick(param: Map<String, dynamic>.from(call.arguments));
        }
      }
      return Future(() {});
    });
  }

  dealPushClick({Map<String, dynamic>? param}) {
    //处理push点击
  }

  Future<String> sendMsgToNative(String method,
      {Map<String, dynamic>? param}) async {
    jdLog("flutter 开始 native调用了flutter ");
    String result = await tdChannel.invokeMethod(method, param);
    jdLog("flutter 收到native的返回值 $result");
    return result;
  }

  // 获取设备信息
  Future<Map> getAppInfo() async {
    Map info = await tdChannel.invokeMethod("getAppInfo");
    return info;
  }

  jumpToIosAppstore() {
    if (!Platform.isIOS) {
      return;
    }
    jdLog("jumpToIosAppstore");
    tdChannel.invokeMethod("jumpAppStore");
  }
}
